//
//  HealthViewController.m
//  B
//
//  Created by lanou on 16/5/7.
//  Copyright © 2016年 裴启航. All rights reserved.
//

#import "HealthViewController.h"
#import "FitnessTableViewController.h"
#import "FoodViewController.h"
#import "ScrollCarouselView.h"
#import "WebviewViewController.h"
#import "ReadTableViewController.h"
#import "FoodsTableViewController.h"
#import "FitnessVideoCollectionViewController.h"
#import "SportsTableViewController.h"
#import "HealthyViewController.h"

@interface HealthViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSArray *listArrs;
@property (nonatomic, strong) NSArray *urlArrs;
@end

@implementation HealthViewController
- (void)viewWillDisappear:(BOOL)animated {
    self.navigationController.navigationBar.tintColor = [UIColor grayColor];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    NSArray *array = @[@"http://image.enmuo.com/CMS/2013/12/10/1/CMS_131210170750664_0E_600x400.jpg", @"http://s7.sinaimg.cn/mw690/004a5E3dzy6IqEcv2yWc6&690"];
    NSArray *url = @[@"http://food.boohee.com/fb/v1/food_articles/38.html&outside=true0", @"http://food.boohee.com/fb/v1/food_articles/39.html&outside=true"];
    ScrollCarouselView *caView = [[ScrollCarouselView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height / 4) imageUrls:array];
    [self.view addSubview:caView];
    
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake((self.view.frame.size.width - 100) / 2 + 17, caView.frame.size.height + 30 + 64, 100, 30)];
    lable.text = @"热门推荐";
    lable.textAlignment = NSTextAlignmentLeft;
    lable.textColor = [UIColor colorWithRed:1.000 green:0.490 blue:0.391 alpha:1.000];
    [self.view addSubview:lable];
    
    caView.imageClick = ^(NSInteger index) {
        WebviewViewController *infoVC = [[WebviewViewController alloc] init];
        infoVC.string = url[index];
        [self.navigationController pushViewController:infoVC animated:YES];
    };
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((self.view.frame.size.width - 30) / 3, 120);
    layout.minimumInteritemSpacing = 5;
    layout.minimumLineSpacing = 5;
    layout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64 + caView.frame.size.height + 80, self.view.frame.size.width, self.view.frame.size.height - 64 - 20 - caView.frame.size.height) collectionViewLayout:layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [self.view addSubview:self.collectionView];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    
    self.listArrs = @[@"食物库", @"养生减肥", @"健康美食", @"健身视频", @"运动知识", @"计步器"];
    self.urlArrs = @[@"http://img8.007swz.com/cpimg/gaoliangsushu/BLZukdtDZJ_1348482477.jpg", @"http://img.25pp.com/uploadfile/app/icon/20151028/1445980660239121.jpg", @"http://p2.qhimg.com/t0112ea0a1b93323c35.jpg", @"http://img02.tooopen.com/images/20150216/tooopen_sy_110866566975.jpg", @"http://pic.58pic.com/10/81/57/56bOOOPICcd.jpg", @"http://www.kviso.com/soft/UploadPic/2014-7/20147912362417293.jpg"];

}

#pragma mark - collectView代理
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.listArrs.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(25, 5, cell.frame.size.width - 50, cell.frame.size.width - 50)];
    [imageView sd_setImageWithURL:[NSURL URLWithString:self.urlArrs[indexPath.row]]];
    imageView.layer.masksToBounds = YES;
    imageView.layer.borderColor = [[UIColor whiteColor] CGColor];
    imageView.layer.borderWidth = 2.0f;
    imageView.layer.cornerRadius = (cell.frame.size.width - 50) / 2.0f;
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(25, imageView.frame.size.height + 5 + 10, cell.frame.size.width - 50, 30)];
    lable.textAlignment = NSTextAlignmentCenter;
    lable.font = [UIFont fontWithName:@"Helvetica" size:12];
    lable.text = self.listArrs[indexPath.row];
    [cell addSubview:lable];
    [cell addSubview:imageView];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        FoodViewController *search = [[FoodViewController alloc] init];
        [self.navigationController pushViewController:search animated:YES];
    }
    if (indexPath.row == 1) {
        ReadTableViewController *fit = [[ReadTableViewController alloc] init];
        [self.navigationController pushViewController:fit animated:YES];
    }
    if (indexPath.row == 2) {
        FoodsTableViewController *foods = [[FoodsTableViewController alloc] init];
        [self.navigationController pushViewController:foods animated:YES];
    }
    if (indexPath.row == 3) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake((self.view.frame.size.width - 15) / 2, (self.view.frame.size.width - 15) / 2);
        layout.minimumInteritemSpacing = 0;
        layout.minimumLineSpacing = 5;
        layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
        FitnessVideoCollectionViewController *video = [[FitnessVideoCollectionViewController alloc] initWithCollectionViewLayout:layout];
        [self.navigationController pushViewController:video animated:YES];
    }
    if (indexPath.row == 4) {
        SportsTableViewController *detail = [[SportsTableViewController alloc] init];
        [self.navigationController pushViewController:detail animated:YES];
    }
    if (indexPath.row == 5) {
        HealthyViewController *healthy = [HealthyViewController new];
        [self.navigationController pushViewController:healthy animated:YES];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
